package com.nifostasky.comic.config;

import com.nifostasky.comic.common.exception.ExceptionFilter;
import com.nifostasky.comic.common.tokenfilter.TokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterConfig {

    /**
     * 捕获过滤器异常信息
     * @param exceptionFilter
     * @return
     */
    @Bean
    public FilterRegistrationBean exceptionFilterRegistration(@Autowired ExceptionFilter exceptionFilter) {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(exceptionFilter);
        registration.setName("exceptionFilter");
        registration.addUrlPatterns("/*");
        //Order顺序最小,捕获其他所有过滤器异常;
        registration.setOrder(-1);
        return registration;
    }


    /**
     * token验证过滤器,获取登录信息
     * @param tokenFilter
     * @return
     */
    @Bean
    public FilterRegistrationBean tokenFilterRegistration(@Autowired TokenFilter tokenFilter){
        FilterRegistrationBean registration=new FilterRegistrationBean();
       //设置过滤器名称
        registration.setFilter(tokenFilter);
        registration.setName("tokenFilter");
        //配置过滤规则
        registration.addUrlPatterns("/*");
        //拦截器1
        registration.setOrder(1);
        return registration;
    }
}
